<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/3/24 0024
 * Time: 19:53
 */
ini_set('memory_limit','256M');
defined('BASEPATH') OR exit('No direct script access allowed');
include_once('./simple_html_dom.php');


class Youlai_info extends CI_Controller
{

    public function info(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        // 从list拿出来一个，如果执行不成功，写到另外的list里
        $urlid = $redis->lpop('youlai_list_video');
        //$urlid = 1;
        if(!$urlid){
            echo "nodata";
            exit;
        }
        $url_info = $this->db->where('id',$urlid)->get('youlai_voice_url')->row_array();
        if(!$url_info){
            echo "nodata";
            exit;
        }
        //echo "采集开始:".time()."-";
        $detail_url = "https://www.youlai.cn".$url_info['url'];
        //echo $detail_url;
        $content = geturl($detail_url);
        $html = str_get_html($content);
        // 处理对应字段
        $department1 = $url_info['dept1'];
        $department2 = $url_info['dept2'];
        $disease = $url_info['dis'];
        $video_name = $url_info['title'];
        $video_url = $html->find("audio",0)->src;
        $video_content = trim($html->find("div[class=v_con]",0)->find("div[class=text]",0)->innertext);
        $pubdate = trim($html->find("span[class=fl_left time]",0)->innertext);
        $click_num = trim($html->find("span[class=fl_left num]",0)->innertext);
        $click_num = str_replace("收听：","",$click_num);
        $author_name = $html->find("strong",0)->innertext;
        $author_title = $html->find("ul[class=mgBottom10]",0)->find("li",1)->find("span",0)->innertext;
        $author_department = $html->find("dl[class=doc_pic_box pdTop10 clearfix]",0)->find("dd",0)->find("a",0)->find("p",1)->innertext;
        $author_hospital = $html->find("dl[class=doc_pic_box pdTop10 clearfix]",0)->find("dd",0)->find("a",0)->find("p",0)->innertext;

        // 点击数量需要通过python重新获取，接口这边加密了

        // 下载mp3,研究是用oss接口上传
        $this->load->library('aliyun_oss');
        $save_dir = "D:\youlai_voice";
        //echo "下载开始:".time()."-";
        $r = $this->getmp3($video_url,$save_dir.'/',md5($url_info['title']).$url_info['id'].'.mp3');
        //echo "下载结束:".time()."-";
        if($r){
            // 填写Bucket名称，例如examplebucket。
            $bucket= "rzaidata";
// 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
            $object = "youlai_voice/".md5($url_info['title']).$url_info['id'].'.mp3';
// 填写待上传的字符串。
            $file_name = $save_dir.'/'.md5($url_info['title']).$url_info['id'].'.mp3';
            //echo "上传开始:".time()."-";
            $res = $this->aliyun_oss->uploadFile($bucket, $object, $file_name);
            //echo "上传结束:".time()."-";
            if($res["info"]['http_code'] == 200){
                $video_url_local = $object;
                // 移除本地文件
                unlink($file_name);
                // 拼接sql
                $sql = "INSERT ignore INTO ai_spider_youlai_disease_voice(department_1,department_2,disease,voice_name,voice_url,voice_url_local,voice_content,pubdate,click_num,author_name,author_title,author_department,author_hospital,url_id) VALUES ('".$department1."','".$department2."','".$disease."','".$video_name."','".$video_url."','".$video_url_local."','".$video_content."','".$pubdate."','".$click_num."','".$author_name."','".$author_title."','".$author_department."','".$author_hospital."',$urlid)";
                $this->db->query($sql);
                //echo "入库结束:".time()."-";
            }else{
                $redis->rPush('youlai_list_video',$urlid);
            }
        }
        echo 1;
        //$ = $html->find("ul[id=videoList] li");
    }

    function getmp3($url,$save_dir='',$filename='',$refer=''){
        if(trim($url)==''){
            return -1;
        }
        if(trim($save_dir)==''){
            $save_dir='./';
        }
        if(0!==strrpos($save_dir,'/')){
            $save_dir.='/';
        }
        //创建保存目录
        if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
            return -3;
        }
        //获取远程文件所采用的方法
        //if($type){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $img = curl_exec($ch);
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if($statusCode == 200 && $img){
            $fp2=@fopen($save_dir.$filename,'a');
            fwrite($fp2,$img);
            fclose($fp2);
            unset($img,$url);
            return 1;
        }else{
            return 0;
        }

    }

    public function push(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        // 从list拿出来一个，如果执行不成功，写到另外的list里
        //$urlid = $redis->lpop('youlai_list');

    }

    public function arc_info(){

    }
}